package br.com.canvasForAll.model.services;

import java.util.Collection;

import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.inject.Named;

import br.com.canvasForAll.model.entity.Canvas;
import br.com.canvasForAll.model.entity.Postit;
import br.com.canvasForAll.model.repository.CorRepository;
import br.com.canvasForAll.model.repository.PostitRepository;
import br.com.canvasForAll.model.repository.QuadranteRepository;
import br.com.canvasForAll.model.services.interfaces.PostitService;

@Stateless
@Named("postitService")
public class PostitServiceImpl implements PostitService {
	
	@EJB private PostitRepository postitRepository;
	@EJB private QuadranteRepository quadranteRepository;
	@EJB private CorRepository corRepository;
	
	@Override
	public Postit obterPorId(Long id) throws Exception {
		return postitRepository.obterId(id);
	}

	@Override
	public Collection<Postit> obterPostitCanvas(Canvas canvas) throws Exception {
		return postitRepository.obterPostitCanvas(canvas);
	}

	@Override
	public Postit estruturaPostitQuadranteCor(String idQuadrante, String idCor, Postit p) throws Exception {
		p.setQuadrante(quadranteRepository.obterId(new Long(idQuadrante)));
		p.setCor(corRepository.obterId(new Long(idCor)));
		
		if(p.getCor() == null || p.getQuadrante() == null){
			throw new Exception("Informações de COR/QUADRANTE não podem permanecer nulas.");
		}
		
		return p;
	}

	@Override
	public Postit salvarPostit(Postit p) throws Exception {
		if(p.getId() == null)
			postitRepository.criar(p);
		else
			postitRepository.atualizar(p);
			
		return p;
	}

}
